﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Atlanchim.Crm.Mui.Pages.Alertes
{
    public class PageEditAlerteModel : AbstractPageEditModel, INotifyPropertyChanged
    {
        /// <summary>
        /// L'alerte en cours
        /// </summary>
        public Alerte Alerte { get; set; }

        /// <summary>
        /// La liste de tous les contacts
        /// </summary>
        public List<Contact> ContactsList { get; set; }

        /// <summary>
        /// FullName du Contact
        /// </summary>
        public string ContactFullName
        {
            get
            {
                if (this.Alerte.Contact == null)
                {
                    return "Indéfini";
                }
                else
                {
                    return string.Format("{0} {1}", this.Alerte.Contact.Nom, this.Alerte.Contact.Prenom);
                }
            }
        }

        /// <summary>
        /// Autorisation d'association de contact à l'alerte
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnCanAssociateContact(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is Contact;
        }

        /// <summary>
        /// Association du contact à l'alerte
        /// </summary>
        /// <param name="target"></param>
        /// <param name="e"></param>
        public void OnAssociateContact(object target, ExecutedRoutedEventArgs e)
        {
            if (e.Parameter is Contact)
            {
                this.Alerte.Contact = (Contact)e.Parameter;
                OnPropertyChanged("ContactFullName");
            }
        }

        /// <summary>
        /// Chargement des données
        /// </summary>
        public override void InnerLoad()
        {
            if (!string.IsNullOrEmpty(this.Fragment))
            {
                // Récupération des Ids
                int idAlerte = 0;
                int.TryParse(this.DicFragments["IdAlerte"], out idAlerte);


                // Chargement ou création de l'entité
                if (idAlerte > 0)
                {
                    this.Alerte = this._pageContext
                                        .Alertes
                                        .Include("Contact")
                                        .Where(alerte => alerte.Id == idAlerte)
                                        .FirstOrDefault();
                }
                else
                {
                    this.Alerte = new Alerte();
                    this.Alerte.Date = DateTime.Now.Date;
                    this._pageContext.Alertes.Add(this.Alerte);
                }

                // Chargement des Contacts
                this.ContactsList = this._pageContext
                                        .Contacts
                                        .ToList();
            }
        }
    }
}
